;
(function() {
	var AppConfigBase = require("./config.template/config.base.template");
	var fs = require('fs');
	var path = require('path');
	var qiniu = require("./qiniu");
	// var multiparty = require('multiparty');
	var domain = require("domain");


	var config_json_url = __dirname + "/ueditor/config.json";
	var config_json = fs.readFileSync(config_json_url).toString();
	// var router_path = "/js/lib/ueditor/";
	var static_dir = path.join(__dirname, '../temp');
	var visit_dir_path = "/upload";

	function _responseData(data, cb) {
		if (cb) {
			return cb + "(" + data + ")";
		} else {
			return data;
		}
	};
	module.exports = {
		prefix: "/ueditor",
		get: {
			"/": function() {
				// this.body = config_json;
				console.log("get config");
				var query = this.request.query;
				if (query.callback) {
					this.body = query.callback + "(" + config_json + ")";
				} else {
					this.body = config_json;
				}
			},
		},
		post: {
			"/": function() {
				var self = this;
				var req = this.request;
				var res = this.response;
				console.log("uploadimage!!!!!!");
				if (req.query.action == "uploadimage") {
					var title = req.body.pictitle;
					var img = req.files.upfile;
					// var img = files.upfile[0];
					var imgName = (+new Date().getTime()) + img.originalFilename;
					// console.log(static_dir,visit_dir_path,img.path,imgName,img);

					var dir_save_path = static_dir + visit_dir_path;
					var img_save_path = static_dir + visit_dir_path + "/" + imgName;
					var img_visit_path = visit_dir_path + "/" + imgName;

					qiniu.upload(img.path, function(error, ret) {
						//上传完成后，删除缓存文件
						fs.unlink(img.path);
						if (error) {
							self.body = _responseData(JSON.stringify({
								'message': "上传权限有误",
								'state': 'ERROR'
							}), req.query.callback);
							return;
						}
						var host = req.header("host");
						var base_config = AppConfigBase(host);
						var result = {
							'url': base_config.img_server_url + ret.key,
							'title': title,
							'original': img.originalFilename,
							'state': 'SUCCESS'
						};
						// res.json(result);
						self.body = _responseData(JSON.stringify(result), req.query.callback);
					});
				} else if (req.query.action == "listimage") {
					// var str = '';
					// var i = 0;
					// var list = [];
					// fs.readdir(static_dir + visit_dir_path, function(err, files) {
					// 	if (err) throw err;


					// 	var total = files.length;

					// 	files.forEach(function(file) {
					// 		var temp = {};
					// 		temp.url = visit_dir_path + "/" + file;
					// 		list[i] = (temp)
					// 		i++;

					// 		// send file name string when all files was processed
					// 		if (i === total) {

					// 			res.json({
					// 				"state": "SUCCESS",
					// 				"list": list,
					// 				"start": 1,
					// 				"total": total
					// 			})
					// 		}
					// 	});
					// });
					self.body = _responseData(JSON.stringify({
						"state": "SUCCESS",
						"list": [],
						"start": 1,
						"total": 0
					}), req.query.callback);
				}
			}
		},
	}
}());